var app = angular.module("addApp", []);

var timeout_json = function(data, $window) {
	if(data.returnCode == 999994) {
		var ss = 3;
		layer.msg(data.returnMessage + "！<strong class='Ajax_Out'>" + ss + "</strong> 秒后前往登录页", 50, -1);
		var picInt = setInterval(function() {
			if(ss >= 1) {
				ss = ss - 1;
				$('.Ajax_Out').text(ss);
			} else {
				clearInterval(picInt);
				try {
					$window.parent.location.href = data.returnData;
					$window.location.href = data.returnData;
				} catch(e) {
					log('timeout!')
				}
			}
		}, 1000);
		return false;
	}
}

app.config(function($httpProvider) {
	$httpProvider.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded';
	$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
	$httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
});

app.controller('editCtrl', function($scope, $http, $window, $timeout) {
	var vm = $scope.vm = content = $scope.content = {
			domain_id: null,
			domain_name: null,
			domain_domain: null,
			domain_category: null,
			tagvalue_id: null
		}
		//新增
	var avoidDup = true;
	$scope.domAjax = function(e) {
		if(e == 'save') {
			if(avoidDup==false){
				return false;
			}
			//从页面取出站点类型
			$scope.vm.domain_category = $("#domain_category").val();
			var tagvalue_id = $("#tagvalue_id").attr("ids");
			var params = {
				domain_name: $scope.vm.domain_name,
				domain_domain: $scope.vm.domain_domain,
				domain_category: content.domain_category,
				tagvalue_id: tagvalue_id
			};
			if(params.domain_category == "新闻") {
				params.domain_category = "News";
			} else if(params.domain_category == "电子报") {
				params.domain_category = "EPaper";
			} else if(content.domain_category == "研报") {
				content.domain_category = "ResearchReport";
			} else if(content.domain_category == "公告") {
				content.domain_category = "PublicNotice";
			} else if(content.domain_category == "微信") {
				content.domain_category = "WeChat";
			} else if(content.domain_category == "微博") {
				content.domain_category = "Weibo";
			} else if(content.domain_category == "论坛") {
				content.domain_category = "BBS";
			} else if(content.domain_category == "博客") {
				content.domain_category = "BLOG";
			} else if(content.domain_category == "视频") {
				content.domain_category = "LIVE";
			}
			
			//验证站点分类
			$scope.flagSelectType = false;
			if($("#domain_category").val()!="" && $scope.vm.domain_name!=null && $scope.vm.domain_domain!=null){
//				//验证url地址
//				var strReg = "^((https|http|ftp|rtsp|mms)?://)(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\&%_\./-~-]*)?";
//				var re = new RegExp(strReg);
//				if(!re.test($.trim(params.domain_domain))) {
//					layer.msg("请输入有效的站点网址",2,-1);
//					return false;
//				}
				avoidDup =false;
				$.post(basePath + 'domain/addDomain.do',params).success(function(data){
					$scope.content = data.returnData;
					var j=timeout_json(data,$window);
					if(j==false){ 
						return false;
					}
					if(data.returnCode==10000){
						layer.msg(data.returnMessage, 1, 10);
						$timeout(function(){
							var index = parent.layer.getFrameIndex(window.name);
							try{
								window.parent.$('#Msearch').trigger('click');
							}catch(e){}
							parent.layer.close(index);
						},1000);
					}
					else{ 
						layer.msg(data.returnMessage, 2, -1);
						avoidDup =true;
						return false;
					}
				});
			} else {
				if($("#domain_category").val()==""){
					$scope.flagSelectType = true;
				}
			}
		} else if(e == 'nosave') {
			function closeWP() {
				var Browser = navigator.appName;
				var indexB = Browser.indexOf('Explorer');
				if(indexB > 0) {
					var indexV = navigator.userAgent.indexOf('MSIE') + 5;
					var Version = navigator.userAgent.substring(indexV, indexV + 1);

					if(Version >= 7) {
						window.open('', '_self', '');
						window.close();
					} else if(Version == 6) {
						window.opener = null;
						window.close();
					} else {
						window.opener = '';
						window.close();
					}
				} else {
					window.close();
				}
			}
			closeWP();
			return false;
		}
	};
});

//open edit Layer
var editExaDataLayer = null;
var ruleSelectType = 0;
$('.editExaDataBtn').live('click', function() {
	var editExaDataLayer = $.layer({
		type: 1,
		title: false,
		area: ['700', 'auto'],
		border: [0], //去掉默认边框
		shade: [0.3, '#000'], //去掉遮罩
		closeBtn: [0, false], //去掉默认关闭按钮
		fadeIn: 500,
		move: "#editExaData_tile",
		page: {
			dom: '#editExaDataLayer'
		}
	});
	//自设关闭
	$('#editExaDataLayer > .closeBtn,#editExaDataLayer .cancelLayer,#editExaSubmitBtn').live('click', function() {
		layer.close(editExaDataLayer);
	});
});

var edit = {
	"qCompanyList": function(_this, siteArr, jsons) {
		var _obj = $("#CustomsAreaList");
		var params = {
			pageIndex: 1,
			pageSize: 1000

		}
		$.ajax({
			type: "POST",
			url: basePath + "Tagvalue/queryTagvalue.do?pageIndex=" + params.pageIndex + "&pageSize=" + params.pageSize,
			data: params,
			success: function(data) {
				if(data.returnCode == 10000) {
					var s2 = data.returnData.length;
					if(s2 > 0) {
						var htmls = "";
						var checkedHtmls = "";
						for(var i = 0; i < s2; i++) {
							var checkedBtn = "";
							var checked = "";
							var tagvalue_id = data.returnData[i].tagvalue_id;
							var checkeddefault = "";
							var num = siteArr.length;
							var tf = null;
							for(var nn = 0; nn < num; nn++) {
								if(tagvalue_id == siteArr[nn]) {
									checkedBtn = "true";
									checked = "checked";
									checkeddefault = "true";
									tf = true;
									break;
								} else {
									checkedBtn = "false";
									checkeddefault = "false";
									checked = "";
									tf = false;
								}
							}
							if(tf == true) {
								checkedHtmls += "<div class='infoLlist' title='" + data.returnData[i].tag_type +"-"+data.returnData[i].tag_value + "'><span class='ui_checkbox " + checked + "' tagv-id= '" + data.returnData[i].tagvalue_id + "' checkedBtn='" + checkedBtn + "' checkeddefault='" + checkeddefault + "'></span> <font>" + data.returnData[i].tag_type +"-"+ data.returnData[i].tag_value + "</font></div>";
							} else if(tf == false) {
								htmls += "<div class='infoLlist' title='" + data.returnData[i].tag_type +"-"+data.returnData[i].tag_value + "'><span class='ui_checkbox " + checked + "' tagv-id= '" + data.returnData[i].tagvalue_id + "' checkedBtn='" + checkedBtn + "' checkeddefault='" + checkeddefault + "'></span> <font>" + data.returnData[i].tag_type +"-"+data.returnData[i].tag_value + "</font></div>";

							}
						}
						_obj.html(checkedHtmls + htmls + "<div class='clear'></div>");
						checkTrueFalse();
					}
					if(s2 < 1) {
						_obj.html("<br><bR><p align='center'>很遗憾，没有查询到相关的数据！</p><br><bR>");
					}
				} else if(data.returnCode == 999994) {
					setUserTimeout(data);
				} else {
					layer.msg(data.returnMessage, 2, -1);
				}
			},
			error: function(message) {
				layer.msg("操作失败，请重试！", 2, -1);
			},
			beforeSend: function() {
				var imgGif = "<p class='loadingGif'></p>";
				_obj.html(imgGif);
			}
		});
	},
	"quyeryCheck": function(ids, names) {
		var $selectShowSpan = $(".editExaDataBtn");
		var $showSpan = $selectShowSpan.parent().prev("span");
		$selectShowSpan.attr({
			ids: ids
		});
		$showSpan.attr({
			ids: ids
		});
		$showSpan.text(names);
	}
}
$(".infoLlist .ui_checkbox").live("click", function() {
	if($(this).attr("checkedBtn") == "true") {
		$(this).attr("checkedBtn", "false");
		$(this).removeClass("checked");
	} else {
		$(this).attr("checkedBtn", "true");
		$(this).addClass("checked");
	}
});
$(".editExaDataBtn").live("mouseover", function() {
	var strId = $(this).attr('ids');
	var siteArr = strId.split(",");
	edit.qCompanyList($(this), siteArr, "");
});

$("#tagvalueQueryBtn").live("click", function() {
	publicQDataList3($("#tagvName"));
});
$('.enterKeyDownElement2').keydown(function(e) {
	if(e.keyCode == 13) {
		publicQDataList3($("#tagvName"));
	}
});

function publicQDataList3(_this) {
	var kV = $.trim(_this.val());
	$("#CustomsAreaList .infoLlist").each(function() {
		var name = $(this).children("font").text();
		var isOf = $.trim(name.indexOf(kV));
		if(isOf > -1) {
			$(this).show();
		} else {
			$(this).hide();
		}
	});
}
$("#editExaSubmitBtn").live("click", function() {
	var trueArr = new Array();
	var trueStrArr = new Array();
	$("#CustomsAreaList .ui_checkbox").each(function() {
		var id = $(this).attr("tagv-id");
		var name = $(this).next("font").text();
		var checkedbtn = $(this).attr("checkedbtn");
		var checkeddefault = $(this).attr("checkeddefault");
		if(checkedbtn == "true") {
			trueArr.push(id);
			trueStrArr.push(name);
		};
	});
	var trueArrs = "";
	var trueStrArrs = "";
	if(trueArr.length > 0) {
		trueArrs = trueArr.join(",");
		trueStrArrs = trueStrArr.join(",");
	}
	edit.quyeryCheck(trueArrs, trueStrArrs);
	$(".xubox_main").css("cssText", "height:45px!important; z-index:198916;");
});

$(".selectAllAreaGoGo .allSel").live("click", function() {
	var checkedbtn = $(this).attr("checkedbtn");
	if(checkedbtn == "true") {
		$("#CustomsAreaList .infoLlist .ui_checkbox").addClass("checked").attr("checkedbtn", "true");
	} else {
		$("#CustomsAreaList .infoLlist .ui_checkbox").removeClass("checked").attr("checkedbtn", "false");
	}
});

$(".selectAllAreaGoGo .reDefault").live("click", function() {
	$("#CustomsAreaList .infoLlist .ui_checkbox").each(function() {
		var checkeddefault = $(this).attr("checkeddefault");
		if(checkeddefault == "true") {
			$(this).addClass("checked").attr("checkedbtn", "true");
		} else {
			$(this).removeClass("checked").attr("checkedbtn", "false");
		}
	});
	checkTrueFalse();
});
$("#CustomsAreaList .infoLlist .ui_checkbox").live("click", function() {
	checkTrueFalse();
});

function checkTrueFalse() {
	var isTrue = "true";
	$("#CustomsAreaList .infoLlist .ui_checkbox").each(function() {
		var checkedbtn = $(this).attr("checkedbtn");
		if(checkedbtn == "false") {
			isTrue = "false";
			return;
		}
	});
	if(isTrue == "false") {
		$(".selectAllAreaGoGo .allSel").removeClass("checked").attr("checkedbtn", "false");
	} else {
		$(".selectAllAreaGoGo .allSel").addClass("checked").attr("checkedbtn", "true");
	}
}